package com.eduadmin.controller;

import com.alibaba.excel.EasyExcel;
import com.eduadmin.entity.Page;
import com.eduadmin.entity.StatisticsParamiter;
import com.eduadmin.listener.StatisticsParamiterDataUploadListener;
import com.eduadmin.service.IExamService;
import com.eduadmin.service.IStatisticsParamiterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StatisticsParamiterController {

    //构造函数自动装配
    private final IStatisticsParamiterService statisticsParamiterService;
    private final IExamService examService;

    @Autowired
    public StatisticsParamiterController(IExamService examService, IStatisticsParamiterService statisticsParamiterService) {
        this.statisticsParamiterService = statisticsParamiterService;
        this.examService = examService;
    }

    //导入统计项目名称
    @RequestMapping(value = "addStatisticsParamiters")
    @ResponseBody
    public String addStatisticsParamiters(MultipartFile statisticsParamiterFile) throws IOException {
        EasyExcel.read(statisticsParamiterFile.getInputStream(), StatisticsParamiter.class, new StatisticsParamiterDataUploadListener(examService, statisticsParamiterService)).sheet().doRead();
        return "上传成功";
    }

    //新增
    @RequestMapping(value = "newAddStatisticsParamiter")
    @ResponseBody
    public Map<String, String> newAddStatisticsParamiter(StatisticsParamiter statisticsParamiter, HttpServletRequest request) {
        Map<String, String> ret = new HashMap<>();

       StatisticsParamiter sp= statisticsParamiterService.getStatisticsParamiterByParams(
                statisticsParamiter.getSemesterName(),statisticsParamiter.getGradeName(),statisticsParamiter.getExamName(),statisticsParamiter.getCategoryName());
        if (sp != null) {
            //已经在数据库中
            ret.put("type", "error");
            ret.put("msg", "增加失败，该考试名称已经在系统中！");
            return ret;
        }
        if (1 != statisticsParamiterService.addOneStatisticsParamiter(statisticsParamiter)) {
            //新增失败
            ret.put("type", "error");
            ret.put("msg", "增加失败！");
        } else {
            //新增成功
            List<StatisticsParamiter> statisticsParamiters = statisticsParamiterService.getAllStatisticsParamiter();
            request.getSession().setAttribute("statisticsParamiters", statisticsParamiters);
            ret.put("type", "success");
            ret.put("msg", "增加成功！");
        }
        return ret;
    }

    //编辑增加 hgm
    @RequestMapping(value = "addEditStatisticsParamiter")
    @ResponseBody
    public Map<String, String> addEditStatisticsParamiter(StatisticsParamiter statisticsParamiter, HttpServletRequest request) {
        Map<String, String> ret = new HashMap<>();
        StatisticsParamiter sp= statisticsParamiterService.getStatisticsParamiterByParams(
                statisticsParamiter.getSemesterName(),statisticsParamiter.getGradeName(),statisticsParamiter.getExamName(),statisticsParamiter.getCategoryName());

        if (sp!=null && sp.getParamiterId()==statisticsParamiter.getParamiterId()) {
            //考试名称存在 更新信息
            statisticsParamiterService.updateStatisticsParamiter(statisticsParamiter);
            ret.put("type", "success");
            ret.put("msg", "修改信息成功！");
        } else {
            //新增信息
            statisticsParamiterService.addOneStatisticsParamiter(statisticsParamiter);
            ret.put("type", "success");
            ret.put("msg", "增加信息成功！");
        }
        List<StatisticsParamiter> statisticsParamiters = statisticsParamiterService.getAllStatisticsParamiter();
        request.getSession().setAttribute("statisticsParamiters", statisticsParamiters);
        return ret;
    }
    //删除信息
    @RequestMapping(value = "deleteStatisticsParamiter")
    @ResponseBody
    public Map<String, String> deleteStatisticsParamiter(@RequestParam(value = "ids[]") String[] paramiterId) {
        //将paramiterId拼接为 ，分割的字符串 方便数据库删除操作
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<paramiterId.length;i++){
            sb.append("'"+paramiterId[i]+"'");
            if(i<paramiterId.length-1){
                sb.append(",");
            }
        }
        String paramiterIdString = sb.toString();
        Map<String, String> ret = new HashMap<>();
        int n = statisticsParamiterService.deleteStatisticsParamiter(paramiterIdString);
        if (n != 0) {
            ret.put("type", "success");
            ret.put("msg", "删除教师成功！");
            return ret;
        }
        ret.put("type", "error");
        ret.put("msg", "删除教师失败！");
        return ret;
    }

    //获取所有
    @RequestMapping(value = "getAllStatisticsParamiter")
    @ResponseBody
    public Map<String, Object> getAllStatisticsParamiter(
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "rows", required = false) String rows
            //@RequestParam(value = "studentName", required = false, defaultValue = "") String studentName
    ) {
        Page pageBean = new Page(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> reMap = new HashMap<>();                 //查询结果map
        Map<String, Object> paraMap = new HashMap<>();           //查询参数map
        paraMap.put("offset", pageBean.getOffset());
        paraMap.put("rows", pageBean.getRows());

        List<StatisticsParamiter> statisticsParamiterList = statisticsParamiterService.getAllStatisticsParamiters(paraMap);
        Long total = statisticsParamiterService.getAllTotal(paraMap);
        reMap.put("rows", statisticsParamiterList);
        reMap.put("total", total);

        return reMap;
    }

}
